<?php
/**
 * 课程类库.
 * User: mac
 * Date: 2018/6/20
 * Time: 16:39
 */

namespace app\api\controller;


class Course extends Api
{
    //推荐书单
    public function GetHotBooks(){
        try{
            $books = model("Books")->getHotBooks();
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        if(!$books && !is_array($books)){
            return show(false,0,'没有获取到书单数据');
        }

        return show(true,0,'OK',$books);
    }

    //精品课程
    public function news(){
        try {
            //获取精品课程的数据
            $fineCourse = model("Courses")->getFineCourse();
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }
        //判定是否有数据
        if(!$fineCourse){
            return show(false,0,'没有精品课程的数据');
        }

        //拼接返回数据
        $fines = self::res($fineCourse);

        //返回给前端
        return show(true,0,'OK',$fines);
    }

    //推荐课程
    public function hot(){

        try{
            //获取推荐课程的数据
            $groomsCourse = model("Courses")->getGroomCourse();
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        //判定是否有数据
        if(!$groomsCourse){
            return show(false,0,'没有推荐课程的数据');
        }

        //拼接返回数据
        $grooms = self::res($groomsCourse);

        //返回数据到前端
        return show(true,0,'OK',$grooms);
    }


    //课程详情
    public function info(){
        //获取传递的ID
        $id = input('post.id');

        try {
            //根据ID获取该课程的详细信息
            $course = model("Courses")->getCourseById($id);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        if(!$course && !is_array($course)){
            return show(false,0,'没有获取到'.$id."的数据");
        }
        return show(true,0,'',$course);

    }

    //音频列表
    public function resource(){
        //获取传递的ID
        $id = input('post.id');

        //判定ID是否存在
        if(!$id && !is_numeric($id)){
            return show(false,0,'ID不存在');
        }

        //根据ID入库查询
        try{
            $audios = model("Audios")->getAudiosById($id);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        //判定是否有值
        if(!$audios && !is_array($audios)){
            return show(false,0,'没有获取到'.$id."的数据");
        }

        //拼接数据
        $result = array();
        foreach($audios as $k => $v) {
            $result['Audios'][$k]['Id'] = $v['id'];
            $result['Audios'][$k]['IsCanAudition'] = $v['is_audition'] ? true : false;
            $result['Audios'][$k]['IsCanPlay'] = true;//判断用户有没有登陆 购买（待完善）
            $result['Audios'][$k]['Long'] = $v['timelong'];
            $result['Audios'][$k]['ReadCount'] = '';
            $result['Audios'][$k]['Tag'] = $v['tag'];
            $result['Audios'][$k]['Title'] = $v['title'];

        }
        $res['Audios'] = $result;
        //返回数据
        return show(true,0,'',$result);
    }

    //音频详情
    public function path(){
        //获取音频ID
        $audioId = input('post.id');

        //入库操作
        try{
            $course = model("Audios")->getCourseAudioById($audioId);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        //拼接数据
        $result = array(
            'AuditionSecond' => '',
            'CourseId' => $course['CourseId'],
            'CourseTitle' => $course['CourseTitle'],
            'Docment' => $course['content'],
            'ImgUrl' => $course['img_url'],
            'IsBuy' => false,
            'IsCanAudition' => true,
            'NextId' => null,
            'Path' => $course['Path'],
            'PrevId' => '',
            'SortOrder' => $course['sort'],
            'Title' => $course['title']
        );
        return show(true,0,'OK',$result);
    }


    public function Browse(){
        //获取音频ID
        $aid = input('post.id');
        //获取课程ID
        $courseId = input('post.courseid');

        return show(true,0,'');
    }

    public function duration(){
        //获取课程ID
        $courseId = input('post.courseid');
        return show(true,0,'');
    }


    //课程评价
    public function comments(){
        //获取传递的ID
        $id = input('post.CourseId');


        //判定传递过来的ID
        if(!$id && !is_numeric($id)){
            return show(false,0,'ID不存在');
        }

        //获取传递过来的Page
        $page = input('post.pageindex');
        $pageSize = 10;

        //根据ID入库查询
        try{
            $comments = model("Comments")->getCommentsById($id,$page,$pageSize);
            $PageCom = count($comments);
            $count = model("Comments")->getCountById($id);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        //拼接数据
        $result = array(
            'CurrentCount' => $PageCom,
            'IsNextPage' => '',
            'IsPreviousPage' => '',
            'Items' => self::GetComments($comments),
            'PageIndex' => $page,
            'PageSize' => $pageSize,
            'TotalCount' => $count,
            'TotalPages' => '',
        );

        return show(true,0,'',$result);

    }


    //赠送好友操作
    public function GetGiveOrderInfo(){
        //获取赠送的订单ID
        $id = input('post.id');

        //获取赠送人ID
        $cid = input('post.cid');

        try{
            $course = model("GiveOrder")->getGiveOrderCourseById($id);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        //拼接数据
        $res = array(
            'AuthorDesc' => '',
            'AuthorName' => $course['username'],
            'CourseId' => $course['course_id'],
            'CourseName' => $course['title'],
            'Id' => $course['id'],
            'ImageUrlDesc' => $course['img_url'],
            'UnitPrice' => $course['price'],
            'UserName' => $course['name']
        );

        return show(true,0,'OK',$res);
    }


    //邀请好友拼团
    public function GroupOrder(){
        //获取拼团表ID
        $id = input("post.id");

        //入库操作
        try{
            $group = model("Group")->getGroupOrderByID($id);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        //拼接数据
        $result = array(
            'AuthorDesc' => '',
            'AuthorName' => $group['name'],
            'BuyCount' => $group['num'],
            'CourseId' => $group['CourseId'],
            'CourseName' => $group['title'],
            'GroupOrderId' => $group['id'],
            'GroupPrice' => $group['g_price'],
            'hour' => '',
            'ImageUrl' => $group['img_url'],
            'IsCreateOrder' => true,
            'Items'=>[self::getGroupOrderItems($group)] ,
            'JoinNum'=> $group['join_number'],
            'Min' => '',
            'Ordertitle' => $group['OrderTitle'],
            'Price' => $group['price'],
            'Seconds' => '',
            'State' => '',
            'TotalNum' => $group['group_number'],
            'WhenLong' => $group['whenlong']
        );
        return show(true,0,'OK',$result);
    }

    //拼团下单
    public function checksettle(){
        //获取当前用户ID
        $id = $this->cache['id'];
        if(!$id && !is_numeric($id)){
            return show(false,0,'token已获取，请刷新重新获取');
        }
        //获取课程ID
        $courseId = input("post.courseid");

        //入库操作
        try{
            //获取用户的收获地址
            $address = model("Address")->getAddress($id);
            //获取课程的信息
            $course = model("Courses")->getCoursesById($courseId);
        }catch(\Exception $e){
            return show(false,0,$e->getMessage());
        }

        //拼接数据
        $result = array(
            'Address' => self::getAddress($address),
            'AuthorDesc' => '',
            'AuthorName' => $course['name'],
            'GroupPrice' => $course['g_price'],
            'Id' => $course['CourseId'],
            'ImgUrlDesc' => $course['img_url'],
            'Price' => $course['price'],
            'Title' => $course['Title'],
            'TotalHours' => $course['total_hours'],
            'UpdateHours' => $course['update_hours'],
            'whenLong' => $course['whenlong']
        );

        return show(true,0,'',$result);

    }

    //赠送好友立即结算
    public function place2(){
        //获取课程ID
        $cid = input('post.id');
        //获取地址ID
        $addressId = input('post.logisticsId');
        return show(true,0,'',$addressId);
    }

    //拼接赠送好友数据
    public static function getAddress($data){
        if(!$data && !is_array($data)){
            return [];
        }

        $res = array();
        foreach($data as $k => $v){
            $res[$k]['Address'] = $v['detail'];
            $res[$k]['Area'] = $v['address'];
            $res[$k]['Id'] = $v['id'];
            $res[$k]['IsDefault'] = $data['is_default'] ? true : false;
            $res[$k]['PhoneName'] = $v['phone'];
            $res[$k]['UserName'] = $v['name'];
        }
        return $res;
    }






    //拼团中的items
    public static function getGroupOrderItems($data){
        if(!$data && !is_array($data)){
            return [];
        }


        $res['HeadImg'] = $data['image_url'];
        $res['UserName'] = $data['UserName'];

        return json_encode($res);
    }


    //拼接课程评价Item数据
    public static function GetComments($data){
        if(!$data && !is_array($data)){
            return [];
        }
        $res = array();
        foreach($data as $k => $v){
            $res[$k]['Content'] = $v['content'];
            $res[$k]['CreateTimeDesc'] = $v['comment_time'];
            $res[$k]['HeadImg'] = $v['image_url'];
            $res[$k]['IsSelected'] = $v['is_select'];
            $res[$k]['IsTop'] = $v['is_top'];
            $res[$k]['ReplyContent'] = $v['reply'];
            $res[$k]['ReplyTime'] = $v['reply_time'];
            $res[$k]['ReplyTimeDesc'] = '';
            $res[$k]['UserName'] = $v['name'];
        }
        return $res;
    }
    //返回数据拼接
    public static function res($data){
        $res = array();
        foreach($data as $k =>$v) {
            $res[$k]['AuthorDesc'] = null;
            $res[$k]['AuthorName'] = $v['name'];
            $res[$k]['GroupPrice'] = $v['g_price'];
            $res[$k]['Id'] = $v['id'];
            $res[$k]['ImgUrlDesc'] = $v['img_url'];
            $res[$k]['Price'] = $v['price'];
            $res[$k]['Title'] = $v['title'];
            $res[$k]['TotalHours'] = $v['total_hours'];
            $res[$k]['UpdateHours'] = '';
            $res[$k]['WhenLong'] = $v['whenlong'];
        }
        return $res;
    }
}